package com.upcontrol.uartagent.core.util;

import com.upcontrol.uartagent.constant.UartCmdType;
import com.upcontrol.uartagent.entity.ErrorGetPacket;
import com.upcontrol.uartagent.entity.GasGetPacket;
import com.upcontrol.uartagent.entity.PM25GetPacket;
import com.upcontrol.uartagent.entity.RespMotorSpeedPacket;
import com.upcontrol.uartagent.entity.RespSelfCheckPacket;
import com.upcontrol.uartagent.entity.SNGetPacket;
import com.upcontrol.uartagent.entity.SnowOffGetPacket;
import com.upcontrol.uartagent.entity.TempHumGetPacket;
import com.upcontrol.uartagent.entity.TouchGetPacket;
import com.upcontrol.uartagent.entity.UartPacket;


/**
 * Copyright (c) 2016, SuZhou CsjBot. All Rights Reserved. <br>
 * www.csjbot.com<br>
 * <p>
 * Created by 浦耀宗 at 2016/11/07 0007-19:19.<br>
 * Email: puyz@csjbot.com
 */
public class PacketUtil {
    public static UartPacket parse(byte[] data) {
        try {
            byte packetType = data[3];
            switch (packetType) {
                case UartCmdType.RecFromUart.SENSOR_TOUCH:
                    return (TouchGetPacket) new TouchGetPacket().decodeBytes(data);
                case UartCmdType.RecFromUart.SENSOR_TEMPERATURE_HUMIDITY:
                    return (TempHumGetPacket) new TempHumGetPacket().decodeBytes(data);
                case UartCmdType.RecFromUart.SENSOR_COMBUSTIBLE_GAS:
                    return (GasGetPacket) new GasGetPacket().decodeBytes(data);
                case UartCmdType.RecFromUart.SENSOR_PM2_5:
                    return (PM25GetPacket) new PM25GetPacket().decodeBytes(data);
                case UartCmdType.RecFromUart.SNOWBOT_UPBODY_SN:
                case UartCmdType.RecFromUart.SNOWBOT_DOWNBODY_SN:
                    return (SNGetPacket) new SNGetPacket().decodeBytes(data);
                case UartCmdType.RecFromUart.SNOWBOTOFF:
                    return (SnowOffGetPacket) new SnowOffGetPacket().decodeBytes(data);
                case UartCmdType.RecFromUart.FAULT:
                    return (ErrorGetPacket) new ErrorGetPacket().decodeBytes(data);
                case UartCmdType.RecFromUart.UPBODY_SELF_CHECK:
                case UartCmdType.RecFromUart.DOWNBODY_SELF_CHECK:
                    return (RespSelfCheckPacket) new RespSelfCheckPacket().decodeBytes(data);
                //0xA9 获取电机速度数据
                case UartCmdType.RecFromUart.RESP_MOTOR_SPEED:
                    return (RespMotorSpeedPacket) new RespMotorSpeedPacket().decodeBytes(data);
                // FIXME: 2017/5/23  这里是错误的
                default:
                    return null;
            }
        } catch (ArrayIndexOutOfBoundsException e) {
            return null;
        }
    }
}
